const Model = require("../../models");

module.exports = {
  name: "pb_time",

  async add(data) {
    try {
      const res = await Model.models.pb_time.add(data);
      return { ok: 1, data: res };
    } catch (e) {
      console.log(e);
      return { ok: 0 };
    }
  },

  async get_pb_time(query) {
    try {
      const pb_model = Model.models.pb_time;
      const apply_model = Model.models.applys;
      const pb_data = await pb_model.get_pb_time({ d_id: query.d_id });
      const applys = await apply_model.core.findAll(query);
      if (pb_data.length) {
        const type_nums_map = pb_data.reduce((res, cur) => {
          res[cur.time_type] = {
            ...cur.dataValues,
            left_nums: cur.dataValues.nums,
          };
          return res;
        }, {});
        applys.forEach((n) => type_nums_map[n.time_type].left_nums--);
        return { ok: 1, data: type_nums_map };
      } else {
        return { ok: 1, data: [] };
      }
    } catch (e) {
      console.log(e);
      return { ok: 0 };
    }
  },
};
